package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> listCourses(CourseDto courseDto) {
            //1.设置分页参数
        PageHelper.startPage(courseDto.getPage(), courseDto.getPageSize());
        //2.调用mapper查询数据
        List<Course> courseList = courseMapper.selectList(courseDto);
        //3.返回列表数据
        Page<Course> page = (Page<Course>) courseList;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
         courseMapper.deleteById(id);
    }

    @Override
    public void insertCourse(Course course) {
        //设置创建时间和更新时间,补全参数
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public Course selectById(Integer id) {
        Course course = courseMapper.selectById(id);
        return course;
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public List<Course> selectAllCourse() {
        List<Course> courseList = courseMapper.selectAllCourse();

        return courseList;
    }

    @Override
    public List<Course> selectCourseBySubject(Integer subject) {
        List<Course> courseList = courseMapper.selectBySub();
        return courseList;
    }

}
